## 获取日数据


# http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sz002095&scale=60&ma=no&datalen=100
# symbol:股票代码
# scale：分钟线的周期，支持60 30 15 5 没有分钟级别，心态崩了
# ma：均线值，支持10 20 30
# datalen：需要取得的数据条数

# 貌似就是当天的
# https://web.ifzq.gtimg.cn/appstock/app/minute/query?code=sh600519
# https://web.ifzq.gtimg.cn/appstock/app/minute/query?_var=min_data_sh000001&code=sh000001

### 10点半 获取 每天前一小时分钟线，三点半 获取当天的close
import time
from dbtool import MysqlPool
import requests
import json, datetime
from stock_pool import shen_zheng, shang_zheng
import numpy as np
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384:ECDHE:!COMPLEMENTOFDEFAULT"
dbtool = MysqlPool()


def get_minute_data(stock):
    data = {}
    for i in range(3):
        res = requests.get("https://web.ifzq.gtimg.cn/appstock/app/minute/query?code=" + stock, verify=False)
        if res and res.status_code == 200:
            data = json.loads(res.text)
            data = data["data"][stock]["data"]
            break
    return data


i = 0
for s in shang_zheng:
    i = i + 1
    if i > 5:
        break
    # time.sleep(0.1)
    stock = s[-2:].lower() + s[0:6]
    data = get_minute_data(stock)
    day = data["date"]
    fhour = [float(i.split(" ")[1]) for i in data["data"] if int(i[0:4]) < 1030]
    close_f = fhour[-1]
    var_f = round(np.std(fhour), 3)
    print(day, stock, var_f, close_f)
    count = dbtool.execute("insert into quan (tday, symbol, var_f, close_f) values (%s, %s, %s, %s);",
                         (day, stock, var_f, close_f))
    print("count", count)

dbtool.commit()
dbtool.close()
